package com.daniel.mi.result;

/**
 * @Author zengneng46764
 * @Date 2024/3/4 11:25
 * @Since 1.0.0.0
 * @description:全局统一返回结果类
 */

import com.daniel.mi.enumutil.EnumResultCode;
import lombok.Data;

@Data
public class BaseResponse<T> {

    //返回码
    private Integer code;

    //返回消息
    private String message;

    //返回数据
    private T data;

    public BaseResponse(){}

    // 返回数据
    private static <T> BaseResponse<T> build(T data) {
        BaseResponse<T> result = new BaseResponse<T>();
        if (data != null) {
            result.setData(data);
        }
        return result;
    }

    private static <T> BaseResponse<T> build(Integer code, String message) {
        BaseResponse<T> result = new BaseResponse<T>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    private static <T> BaseResponse<T> build(T body, Integer code, String message) {
        BaseResponse<T> result = build(body);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    private static <T> BaseResponse<T> build(EnumResultCode resultCodeEnum, T body) {
        BaseResponse<T> result = build(body);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static<T> BaseResponse<T> success(){
        return BaseResponse.success(null);
    }

    /**
     * 操作成功
     * @param data  baseCategory1List
     * @param <T>
     * @return
     */
    public static<T> BaseResponse<T> success(T data){
        BaseResponse<T> result = build(data);
        return build(EnumResultCode.SUCCESS, data);
    }


    public static<T> BaseResponse<T> fail(Integer code, String message) {
        BaseResponse<T> result = build(code,message);
        return result;
    }



    private BaseResponse<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    private BaseResponse<T> code(Integer code){
        this.setCode(code);
        return this;
    }
}
